<template>
	<div>
		<el-upload class="upload-demo" :on-success="handleAvatarSuccess" :action="action" :headers="headers"
			:before-upload="handleFileChange" :show-file-list="false">
			<el-button class="blue_btn" style="width: 100%;background: #2F6CBC;border: 1px solid #2F6CBC;"
				type="primary">上传简历(一键解析,自动填写)</el-button>
		</el-upload>
	</div>
</template>
<script>
// import {
// 	URL_IP_HOST,
// 	URL_PREFIX
// } from '@/util/constant.js'
import {
	parserResume
} from "@/api/my.js"
export default {
	data() {

		return {
			headers: {},
			action: process.env.BASE_URL + "application/resume/parserResume",
		};
	},
	mounted() {
		this.info()
	},
	methods: {
		info() {
			var userInfoData_ = JSON.parse(sessionStorage.getItem('userInfoData_web'))
			this.headers = {
				"Authorization": `Bearer ${userInfoData_ ? userInfoData_.access_token : ""}`,
				"clientid": userInfoData_ ? userInfoData_.client_id : "eeb9377d4b6debbc0a6e8756bbbbb4c7"
			}
		},
		handleAvatarSuccess(res, file) {
			console.log("文件上传成功：", res, file);
			if (res.code == 200) {
				let obj = {
					type: "解析成功...",
					bool: false,
					data: res.data
				}
				this.$emit('uploadImageClick', obj)
				this.$message({
					message: `解析成功`,
					type: 'success',
					center: true,
					offset: 0.1
				});
			} else {
				let obj = {
					type: "解析失败...",
					bool: false,
				}
				this.$emit('uploadImageClick', obj)
				this.$message({
					message: `${res.msg}`,
					type: 'error',
					center: true,
					offset: 0.1
				});
			}

		},
		handleFileChange(rawFile) {
			console.log(rawFile);
			if (rawFile.type !== 'application/msword' && rawFile.type !== 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' && rawFile.type !== 'application/pdf' && rawFile.type !== 'image/jpg' && rawFile.type !== 'image/png') {
				this.$message({
					message: '格式有误! 仅支持jpg、png、doc、docx、pdf格式!',
					type: 'error',
					offset: 0.1
				});
				return false
			}
			// const file = event.raw;
			// const reader = new FileReader();
			// reader.onload = (e) => {
			// 	// 获取二进制数据流  
			// 	const binaryString = e.target.result;
			// 	// console.log(binaryString); // 打印二进制数据流  
			// 	// 在这里进行后续处理，例如发送到服务器等操作  
			let obj = {
				type: "正在解析...",
				bool: true,
			}
			this.$emit('uploadImageClick', obj)
			// 	let params = {
			// 		fileName: event.name,
			// 		data: binaryString
			// 	}
			// 	parserResume(params).then(res => {
			// 		let obj = {
			// 			type: "解析成功...",
			// 			bool: false,
			// 			data: {}
			// 		}
			// 		this.$emit('uploadImageClick', obj)
			// 	}).catch(res => {
			// 		let obj = {
			// 			type: "解析失败...",
			// 			bool: false,
			// 			data: {}
			// 		}
			// 		this.$emit('uploadImageClick', obj)

			// 	})
			// };
			// reader.readAsBinaryString(file); // 读取文件为二进制字符串  



		}
	}
}
</script>
<style lang="scss" scoped>
.blue_btn {
	background: #2F6CBC;
	border: 1px solid #2F6CBC;
}
</style>
